#include "Timer.h"
#include "Logger.h"
#include "Singulator.h"

namespace Tool
{
	//..............
    // benchmarking
	//..............

	Bench::Bench(bool p_verbose) : m_verbose(p_verbose)
	{
		if (m_verbose) 
		{
			LOGINFO("start bench");
		}

		this->start();
	}

	Bench::~Bench()
	{
		this->stop();

		if (m_verbose)
		{ 
		    LOGINFO("stop bench");
		    LOGINFO("elapsed time = %f", this->elapsedTime());
		}
	}


	void Bench::start()
	{
#ifdef _WIN32
		QueryPerformanceCounter(&m_start); // tick count
#else
		m_start = SDL_GetTicks(); // ms
#endif
	}

	void Bench::stop()
	{
#ifdef _WIN32
		QueryPerformanceCounter(&m_stop); // tick count
#else
		m_stop = SDL_GetTicks(); // ms
#endif
	}

	double Bench::elapsedTime() const
	{
#ifdef _WIN32

		LARGE_INTEGER frequency;

		// get tick counts per seconds
		QueryPerformanceFrequency(&frequency); 

		return (static_cast<double>(m_stop.QuadPart - m_start.QuadPart) / frequency.QuadPart );
#else
		return (m_stop - m_start) / 1000.0; // in seconds
#endif
	}


	//.............
	// timer event
	//.............

	void Timer::setTimeOutEvent(long p_event)
	{
	}

		
	void Timer::setTimeOutValue(long p_timeOut)
	{
	}

	void Timer::setTimeOutCb( void (*p_timeOutCb)(long) )
	{
		try
		{
			ERROR_ASSERT(NULL != p_timeOutCb);
		}

		catch(Exception& e)
		{
			if (EX_FATAL == e) ABORT_APP;
		}
	}

	void Timer::start()
	{
	}

	void Timer::kill()
	{
	}


};
